package com.luxsoft.sw3.replica;

import org.apache.log4j.Logger;
import org.hibernate.StatelessSession;
import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostDeleteEventListener;
import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PostUpdateEventListener;

/**
 * Spring managed bean que se encarga de registrar en la bitacora
 * de entidades replicables
 * 
 * @author Ruben Cancino Ramos
 *
 */
public class ReplicationLogger implements PostInsertEventListener
										,PostUpdateEventListener
										,PostDeleteEventListener{
	
	protected Logger logger=Logger.getLogger(getClass());
	
	
	
	
	

	public void onPostInsert(PostInsertEvent event) {
		
		/*if(event.getEntity() instanceof Replicable){
			try {
				Class clazz=event.getEntity().getClass();
				ReplicaLogRecord record=new ReplicaLogRecord(clazz,event.getId().toString(),"ALTA");
				// need to have a separate session for audit save
		        StatelessSession session = event.getPersister().getFactory().openStatelessSession();
		        session.beginTransaction();
				
				record.sucursal=1L;
				session.insert(record);

				session.getTransaction().commit();
	            session.close();

				logger.info("Log de Replica registrado: "+record);
			} catch (Exception e) {
				logger.error("Error registrando bitacora para replica de tipo ALTA  en: "+event,e);
			}
		}*/		
	}	

	public void onPostUpdate(PostUpdateEvent event) {
		
		/*if(event.getEntity() instanceof Replicable){
			try {
				Class clazz=event.getEntity().getClass();
				
				ReplicaLogRecord record=new ReplicaLogRecord(clazz,event.getId().toString(),"CAMBIO");
				// need to have a separate session for audit save
		        StatelessSession session = event.getPersister().getFactory().openStatelessSession();
		        session.beginTransaction();
				
				record.sucursal=1L;
				session.insert(record);

				session.getTransaction().commit();
	            session.close();

				logger.info("Log de Replica registrado: "+record);
			} catch (Exception e) {
				logger.error("Error registrando bitacora para replica de tipo CAMBIO  en: "+event,e);
			}
		}*/			
	}

	public void onPostDelete(PostDeleteEvent event) {
		/*if(event.getEntity() instanceof Replicable){
			try {
				Class clazz=event.getEntity().getClass();
				
				ReplicaLogRecord record=new ReplicaLogRecord(clazz,event.getId().toString(),"BAJA");
				// need to have a separate session for audit save
		        StatelessSession session = event.getPersister().getFactory().openStatelessSession();
		        session.beginTransaction();
				
				record.sucursal=1L;
				session.insert(record);

				session.getTransaction().commit();
	            session.close();

				logger.info("Log de Replica registrado: "+record);
			} catch (Exception e) {
				logger.error("Error registrando bitacora para replica de tipo ALTA  en: "+event,e);
			}
		}*/			
	}
	

}
